package thread;

public class SyncDemo1 {
    private static boolean success1 = false;
    private static boolean success2 = false;

    public static void main(String[] args) {
        Bank bank = new Bank();
        int sum = 0;
        while (true) {
            sum++;
            Thread t1 = new Thread("张三") {
                @Override
                public void run() {
                    success1 = bank.getMoney(20000);
                }
            };
            Thread t2 = new Thread("李四") {
                @Override
                public void run() {
                    success2 = bank.getMoney(20000);
                }
            };

            t1.start();
            t2.start();

            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (success1 && success2) {
                System.out.println("都成功");
                System.out.println("尝试了"+sum+"次");
                break;
            } else {
                //System.out.println("正常");
                bank.setAccount(20000);
            }
        }
    }
}
